﻿//Oscar, Sahaj

using System.Web.Mvc;
using System.Collections.Generic;
using FacebookLoginButton.Models;
using System.Data;
using System.Data.Entity;
using Facebook;
using Newtonsoft.Json;
using System;

namespace FacebookLoginButton.Controllers
{
    public class AccountController : Controller
    {
        private FacebookContext db = new FacebookContext();
        [HttpPost]
        public JsonResult FacebookLogin(FacebookLoginModel model)
        {
            Session["uid"] = model.uid;
            Session["accessToken"] = model.accessToken;

            return Json(new { success = true });
        }

        
        public FacebookUser getFBUser(string token)
        {
            //try
            //{
                var client = new FacebookClient(token);
                dynamic fbresult = client.Get("me?fields=id,name,email,first_name,last_name,gender,locale,link,username,timezone,location,picture");
                FacebookUser facebookUser = JsonConvert.DeserializeObject<FacebookUser>(fbresult.ToString());

                //dynamic fbresult = client.Get("me/friends?fields=name,id,picture");
                fbresult = client.Get("me/friends?fields=id,name,email,first_name,last_name,gender,locale,link,username,timezone,location,picture");
                var data = fbresult["data"].ToString();

                var friendlist = JsonConvert.DeserializeObject<List<FacebookUser>>(data);

                if (db.PlutoUser.Find(facebookUser.id) == null)
                {
                    db.PlutoUser.Add(facebookUser);
                    //facebookUser.friendsList = new List<FacebookUserFriend>();
                }
                else
                {
                    facebookUser = db.PlutoUser.Find(facebookUser.id);
                }

                /*foreach (FacebookUser friend in friendlist)
                {
                    if (db.PlutoUser.Find(friend.id) == null)
                    {
                        db.PlutoUser.Add(friend);
                    } 
                }*/

                foreach (FacebookUser friend in friendlist)
                {
                    FacebookUser friendObj;
                    if ((friendObj = db.PlutoUser.Find(friend.id)) == null)
                    {
                        friendObj = friend;
                        db.PlutoUser.Add(friendObj);
                    }
                    
                    var relation = new FacebookUserFriend
                    { 
                        FacebookUser = facebookUser, 
                        FacebookFriend = friendObj,
                        FacebookUserId = facebookUser.id,
                        FacebookFriendId = friendObj.id
                    };

                    //facebookUser.friendsList.Add(relation);


                    if (db.PlutoUserFriend.Find(facebookUser.id, friendObj.id) == null)//relation.FacebookUserId, relation.FacebookFriendId) == null)
                    {
                        db.PlutoUserFriend.Add(relation);
                    }
                }

                //return this.Content("OK");
                db.SaveChanges();
                return facebookUser;
        }

        [HttpGet]
        public ActionResult UserDetails()
        {
            FacebookUser currentUser = getFBUser(Session["accessToken"].ToString());
            //return getFBUser(Session["accessToken"].ToString());
            if (currentUser != null)
                return View(currentUser);
            return this.Content("Error retrieving user Data!");
        }

        [HttpGet]
        public ActionResult ErrorMsg(FacebookUserFriend relation)
        {
            return this.Content(String.Format("Relation - {0} {1} -- {2} {3}", relation.FacebookUser.id, 
                                                                                relation.FacebookUser.name, 
                                                                                relation.FacebookFriend.id, 
                                                                                relation.FacebookFriend.name));
        }
                
    }
}